<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * WorkflowBusinessHistoryor: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\workflow\logic;


/**
 * 审批记录历史流转记录=》逻辑层
 */
class WorkflowBusinessHistory extends WorkflowBase
{
    /**
     * 审批记录历史流转记录=>列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getWorkflowBusinessHistoryList($where = [], $field = true, $order = 'id desc', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelWorkflowBusinessHistory->getList($where, $field, $order, $paginate)->toArray();
        if($paginate===false) $list['data']=$list;
        foreach ($list['data'] as &$row){
            $row['create_user_name']=$this->modelSysUser->getValue(['id'=>$row['create_user_id']],'realname');
            $row['deal_status']=$this->getDealStatus($row['deal_status']);
            $row['business_type_name']=$this->modelWorkflowBusinessType->getValue(['business_type'=>$row['business_type']],'name');
            $row['business_info']=$this->serviceWorkflowBusiness->getBusinessTypeInfo($row['business_type'],$row['business_id']);
        }
        return $list;
    }

    /**
     * 业务关联表=>业务审核表审核记录
     * 主为提供针对=》业务模块查询使用
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getWorkflowBusinessHistoryLog($where = [], $field = true, $order = 'id desc', $paginate = false)
    {
        $list = $this->modelWorkflowBusinessHistory->getList($where, $field, $order, $paginate)->toArray();
        if($paginate===false) $list['data']=$list;
        foreach ($list['data'] as &$row){
            $row['create_user_name']=$this->modelSysUser->getValue(['id'=>$row['create_user_id']],'realname');
            $row['curr_node_name']=$this->modelWorkflowNode->getValue(['id'=>$row['curr_node_id']],'name');
            $row['deal_status_name']=$this->getDealStatus($row['deal_status'])['name'];
            $row['sign_type_name']=$this->modelWorkflowBusinessHistory->sign_type_name($row['sign_type']);
        }
        return $list;
    }

    /**
     * 审批历史记录=》添加
     * @param array $data
     * @return array
     */
    public function workflowBusinessHistoryAdd($data = [])
    {
        $validate_result = $this->validateWorkflowBusinessHistory->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateWorkflowBusinessHistory->getError()];
        }
        $data['create_user_id']=empty($data['create_user_id'])?SYS_USER_ID:$data['create_user_id'];

        $result = $this->modelWorkflowBusinessHistory->setInfo($data);

        $result && action_log('新增', '新增审核业务关联表记录表：' . $data['business_type'].',id='.$data['business_id'].',id='.$data['deal_user_name']);
        $url = url('show');
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelWorkflowBusinessHistory->getError()];
    }


    /**
     * 审批历史记录=》详细页
     *
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getWorkflowBusinessHistoryInfo($where = [], $field = true)
    {
        //当前记录
        $info= $this->modelWorkflowBusinessHistory->getInfo($where, $field);
        is_object($info) && $info=$info->toArray();

        //审批流程节点
        $map['workflow_id']=['=',$info['workflow_id']];
        $wflist=$this->modelWorkflowNode->getList($map,"name",'sort asc',false);
        is_object($wflist) && $wflist=$wflist->toArray();
        $info['workflow_list']=arr2str(array_column($wflist,'name'),' → ');

        $info['curr_node_name']=$this->modelWorkflowNode->getValue(['id'=>$info['curr_node_id']],"name");
        $info['deal_status_arr']=$this->getDealStatus($info['deal_status']);

        //审批历史记录
        $history_list= $this->modelWorkflowBusinessHistory->getList(['workflow_business_id' => $info['workflow_business_id']],'','id desc',false);
        foreach ($history_list as &$row){
            $row['curr_node_name']=$this->modelWorkflowNode->getValue(['id'=>$row['curr_node_id']],'name');
            $row['deal_status_name']=$this->getDealStatus($row['deal_status'])['name'];
            $row['sign_type_name']=$this->modelWorkflowBusinessHistory->sign_type_name($row['sign_type']);
        }
        $info['workflow_business_history'] =$history_list;

        //业务信息
        $info['business_type_name']=$this->modelWorkflowBusinessType->getValue(['business_type'=>$info['business_type']],'name');
        $info['business_info']=$this->serviceWorkflowBusiness->getBusinessTypeInfo($info['business_type'],$info['business_id']);

        return $info;
    }


    /**审批处理状态
     * @param null $key
     * @return mixed
     * Author: 开发人生 goodkfrs@qq.com
     * Date: 2022/2/18 0018 16:39
     */
    public function  getDealStatus($key=null){
        return $this->modelWorkflowBusinessHistory->deal_status($key);
    }

    /**
     * 获取列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where=[];

        if(!empty($data['listtype'])){
            if($data['listtype']=='self'){
                $where['deal_user_id'] = ['=', SYS_USER_ID];
            }
        }

        //是否自处理人
        if(isset($data['deal_user_id'])){
            if (!empty($data['deal_user_id']) || is_numeric($data['deal_user_id']) === true) {
                $where['deal_user_id'] = ['=', $data['deal_user_id']];
            }
        }

        //关键字查
        !empty($data['keywords']) && $where['deal_user_name|suggestion_feedback'] = ['like', '%' . $data['keywords'] . '%'];

        //处理状态
        if (!empty($data['deal_status']) || is_numeric($data['deal_status']) === true) {
            $where['deal_status'] = ['=', $data['deal_status']];
        }
        //状态
        if (!empty($data['business_type'])) {
            $where['business_type'] = ['=', $data['business_type']];
        }
        //创建时间
        if (!empty($data['create_rangedate'])) {
            $range_date = str2arr($data['create_rangedate'], "-");
            foreach ($range_date as &$date){
                $date=strtotime($date);
            }
            $where['create_time'] = ['between', $range_date];
        }
        //开始时间
        if (!empty($data['deal_rangedate'])) {
            $range_date = str2arr($data['deal_rangedate'], "-");
            $where['deal_time'] = ['between', $range_date];
        }


        return $where;
    }

    /**
     * 获取排序条件
     */
    public function getOrderBy($data = [])
    {
        $order_by = "";
        if (!empty($data['orderField'])) {
            $orderField = $data['orderField'];
            $orderDirection = $data['orderDirection'];
        } else {
            $orderField = "";
            $orderDirection = "";
        }
        if ($orderField == 'by_link') {
            $order_by = "link_time $orderDirection";
        } else if ($orderField == 'by_next') {
            $order_by = "next_time $orderDirection";
        } else if ($orderField == 'by_nodays') {
            $order_by = "nodays $orderDirection";
        } else {
            $order_by = "id desc";
        }
        return $order_by;
    }
}
